<script>
    common.setMainTitle('Quản trị vai trò' + '<div>&nbsp;</div>');
</script>

<form id="mainform" name=frm method=post action="/admin/roles/input">
    <div>
		<div id="toolbar-button">
			<div class="item" onclick="add()" name="addButton" id="addButton" title="Add">
				<div id="addnew-global-button"></div>
			</div>
			<div class="item" onclick="deleteAll()" name="addButton" id="deleteButton" title="Delete">
				<div id="delete-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(1)" name="activateButton" id="activateButton" title="Activate">
				<div id="save-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(0)" name="deactivateButton" id="deactivateButton" title="Deactivate">
				<div id="back-global-button"></div>				
			</div>
		</div>
		</div>
	</div>
	<table style="margin-top: 10px;clear: both;" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr>
                <th width="3%" class="table-header-repeat line-left" style="text-align: center">
                    <input type=checkbox name=DELALL onclick="SelectAll(this,'ITEM')">
                </th>
                <th width="10%" class="table-header-repeat line-left" style="text-align: left"><a>Name</a></th>
                <th width="32%" class="table-header-repeat line-left" style="text-align: left"><a>Description</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a>Navigation</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a>Status</a></th>
                <th width="" class="table-header-repeat line-left" ><a>Action</a></th>
            </tr>
        </thead>
        <tfoot>
        </tfoot>
        <tbody>
        <script>
            var arrNav=new Array();
        </script>
            <?php
            if(count($this->data)==0){
                echo "<tr><td colspan=6>There is not any data</td></tr>";
            } else {
                $stt = 1;
            }
            $tt = 0;
            //var_dump($this->data);exit;
            foreach($this->data as $item){?>
            <tr style="cursor:pointer">
                <!-- <td style="text-align: center"><?=$stt?></td> -->
                <td style="text-align: center; padding-left: 0px;">
                    <input type="checkbox" name="ITEM[]" value="<?=$item['ROLEID']?>" />
                </td>
                <td>
                    <div>
                        <?=$item['NAME'];?>
                    </div>
                </td>
                <td>
                    <div>
                        <?=$item['DESCRIPTION'];?>
                    </div>
                </td>
                
                <td>
                    <select id="<?php echo 'box'.$tt ?>" name="<?php echo 'box'.$tt ?>" value="" onchange="changeNavigation(this.value,'<?php echo $item['ROLEID'] ?>')">
                   <?php
                  
                    $navigation = '';
                    $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
                    foreach($this->dataSources as $item1){
                        if($item1['description'] !=""){
                            $keys = array_keys($item1['controller']);
                            $navigation .= '<optgroup label="MODULE '.strtoupper($item1['description']).'" style="color:#B8B8B8;font-style:normal">';
                            for($i=0;$i<count($keys);$i++)
                            {
                                $description = $item1['controller'][$keys[$i]]['description'];
                                
                                $des = '';
                                if(strtoupper($description) != strtoupper($keys[$i])){
                                    $des = ' : '.$description;
                                }
                                
                                $navigation .= '<optgroup label="'.$tab.strtolower($keys[$i]).$des.'" value="'.$keys[$i].'" style="color:#004FA0;font-style:normal">';
                                $arr_action = $item1['controller'][$keys[$i]]['action'];
                                $keyAction = array_keys($arr_action);
                                for($ii=0;$ii<count($keyAction);$ii++)
                                {
                                    $selected = "";
                                    $value = $item1['description'].'/'.$keys[$i].'/'.$keyAction[$ii];
                                    if($item['NAVIGATION'] == $value){
                                        $selected = "selected ='true' ";
                                    }
                                    $desAction = '';
                                    if(strtoupper($arr_action[$keyAction[$ii]]) != strtoupper($keyAction[$ii])){
                                        $desAction = ' : '.$arr_action[$keyAction[$ii]];
                                    }
                                    $navigation .= '<option value="'.$value.'" name="'.$item1['description'].'_'.$keys[$i].'_'.$keyAction[$ii].'" style="color:#393939;" '.$selected.' >'.$tab.$tab.$tab.$keyAction[$ii].$desAction.'</option>';
                                }
                                $navigation .= "</optgroup>";
                            }
                            $navigation .= "</optgroup>";
                        }
                    }
                   echo $navigation;
                   ?>
                   </select>
                </td>
				
				<td style="text-align: center">
					<span id="status<?=$item['ROLEID']?>">
                        <? if($item['STATUS']!='1') { ?>
                            <a href="#" onClick="changeStatus(<?=$item['ROLEID'] ?>,1)">Inactive</a>
                        <?} else {?>
                            <a href="#" onClick="changeStatus(<?=$item['ROLEID'] ?>,0)">Active</a>
                        <?}?>
                    </span>
                </td>
	
                <td>               
					<select id="<?php echo 'actionbox'.$item['ROLEID'] ?>" name="<?php echo 'actionbox'.$item['ROLEID'] ?>" onchange="chooseAction(<?=$item['ROLEID'] ?>)">
						<option value="0" >Choose an Action</option>
						<option value="<?=$item['ROLEID'] ?>" >Edit</option>
						<option value="<?=$item['ROLEID'] ?>" >Permission</option>
						<? if($item['ROLEID']>2) {?>
						<option value="<?=$item['ROLEID'] ?>" >Delete</option>
						<? } ?>
						<option value="<?=$item['ROLEID'] ?>" >View Users</option>
					</select>
                </td>
            </tr>
            <?php 
            $stt++;
            $tt++;
            }
            ?> 
        </tbody>
    </table>
</form>
<script type="text/javascript">
    function chooseAction(itemID){
		text = $("#actionbox"+itemID+ " option:selected").text();
		if(text == 'Edit'){
			window.location = "/admin/roles/input/id/"+itemID;
		}else if(text == "Delete"){
			check = confirm("Do you want to delete this role?");
			if(check){
				window.location = "/admin/roles/delete/id/" +itemID;
			}
		}else if(text == "Permission"){
			window.location = "/admin/authorization/index/id/" +itemID;
		}else if(text == "View Users"){
		
		}
    }
	
	function add(){
        window.location = '/admin/roles/input';
    }
	
	function changeNavigation(value,itemID){
        $cnf = confirm('Do you want to change default navigation page (after login) for this role ?');
        if($cnf === true){
            var jxNav = new Jx();
            jxNav.setParameter('URLNAV',value);
            jxNav.setParameter('ROLEID',itemID);
            jxNav.success('completeChangeNavigation');
            jxNav.request('/admin/roles/changenavigation','POST');
        }
    }
	
    function completeChangeNavigation(){
        if(rsChangeNav == 0){
            alert("Thay đổi thất bại \n\n" + rsChangeNavError);
        }
    }
	
    function SelectAll(selobj,childobj){
        var arr = document.getElementsByName(childobj+"[]");
        for(var i=0;i<arr.length;i++){
			arr[i].checked = selobj.checked;
        }
    }
	
	//Gui thong tin status len server
    function changeStatus(itemID, status){
        $.ajax({
			url: "/admin/roles/changestatus",
			type: "POST",
			data: {itemID : itemID, status: status},
			dataType: "text",
			success: function(data) {
				if(status == 0){
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',1)">Inactive</a>');
				} else {
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',0)">Active</a>');
				}
			}
		});
    }
	
	function changeStatusMultiItems(status){
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		var arrValue = new Array();
			
		for(var i = 0 ; i < arr.length ;i++ )
		{
				if(arr[i].checked == true)
				{
						arrValue[i] = arr[i].value;
				}
		}
		for(var i = 0 ; i < arr.length ;i++ )
		{

				if(arr[i].checked == true)
				{
						ln = 1;
						break;
				}
		}
		if(ln == 1)
		{
			var question;
			if(status ==1){
				question = 'Do you want to activate these items?';
			} else {
				question = 'Do you want to inactivate these items?';
			}
			
			if(confirm(question)){
					$.ajax({
						url: "/admin/roles/changestatusmultiitems",
						type: "POST",
						data: {itemIDs : arrValue, status: status},
						dataType: "text",
						success: function(data) {
							for(var i = 0 ; i < arr.length ;i++ )
							{
								if(status == 0){
									$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',1)">Inactive</a>');
								} else {
									$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',0)">Active</a>');
								}
								arr[i].checked=false;
							}
						}
					});
			}
		}
		else
		{
				alert("You have to choose at least 1 item!!");
		}
	}
	
	function deleteAll()
    {
            var ln = 0;
            var arr = document.getElementsByName('ITEM[]');
            for(var i = 0 ; i < arr.length ;i++ )
            {

                    if(arr[i].checked == true)
                    {
                            ln = 1;
                            break;
                    }
            }
            if(ln == 1)
            {
                    if(confirm("<?php echo "Do you want to delete these items?"; ?>")){
                            document.frm.action="/admin/roles/deletemultiitem";
                            document.frm.method="post";
                            document.frm.submit();
                    }
            }
            else
            {
                    alert("You have to choose at least 1 item!!");
            }
    }
</script>